﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Metro.Core;

namespace MetroHome
{
    /// <summary>
    /// Interaction logic for Widget.xaml
    /// </summary>
    public partial class Widget : UserControl
    {
        private Metro.Core.Widget _widget;
        public string Path;

        public string WidgetName
        {
            get;
            set;
        }

        public string WidgetIcon
        {
            get;
            set;
        }

        public bool IsWidgetLoaded
        {
            get;
            set;
        }

        public bool HasErrors
        {
            get;
            set;
        }

        public Widget()
        {
            InitializeComponent();
        }

        public void Initalize(string path)
        {
            this.Path = path;

            Assembly assembly = Assembly.LoadFrom(path);

            Type widgetType = null;

            try
            {
                widgetType = assembly.GetTypes().FirstOrDefault(type => typeof(Metro.Core.Widget).IsAssignableFrom(type));
            }
            catch (Exception ex)
            {
                App.Log(ex.ToString());
            }

            if (widgetType == null)
            {
                App.Log(path + " is not a widget.");
                HasErrors = true;
                return;
            }

            _widget = (Metro.Core.Widget)Activator.CreateInstance(widgetType);
            _widget.AddContextMenuItem += new AddContextMenuItemHandler(WidgetAddContextMenuItem);
            _widget.SetBgBrush += new SetBgBrushHandler(WidgetSetBgBrush);
            /*WidgetName = _widget.GetWidgetName();
            WidgetIcon = _widget.GetIcon();*/
            WidgetName = _widget.Name;
            WidgetIcon = _widget.IconPath;
            if (string.IsNullOrEmpty(WidgetIcon))
                WidgetIcon = App.Path + "\\Resources\\default_icon.png";
            CloseHomeItem.Header = App.LocaleManager.GetString("CloseHome");

        }

        void WidgetSetBgBrush(Brush brush)
        {
            if (brush != null)
                MainGrid.Background = brush;
            else
                MainGrid.Background = (SolidColorBrush)Resources["TileColor"];
        }

        void WidgetAddContextMenuItem(MenuItem item)
        {
            this.ContextMenu.Items.Insert(0, item);
        }

        public void Load()
        {
            _widget.Load();
            //var w = _widget.Load();
            //this.Width = _widget.WidgetControl.Width;
            //this.Height = _widget.WidgetControl.Height;

            Grid.SetColumnSpan(this, Grid.GetColumnSpan(_widget.WidgetControl));
            Grid.SetRowSpan(this, Grid.GetRowSpan(_widget.WidgetControl));

            Grid.SetColumn(this, (int)_widget.Position.X);
            Grid.SetRow(this, (int)_widget.Position.Y);

            MainGrid.Children.Add(_widget.WidgetControl);
        }

        public UserControl GetHub()
        {
            return _widget.HubControl;
        }

        public void Unload()
        {
            MainGrid.Children.Clear();
            int col = Grid.GetColumn(this);
            int row = Grid.GetRow(this);
            _widget.Position = new Point(col, row);
            _widget.Unload();
            IsWidgetLoaded = false;
        }


        private void UserControlLoaded(object sender, RoutedEventArgs e)
        {
            IsWidgetLoaded = true;
        }

        private void CloseHomeItemClick(object sender, RoutedEventArgs e)
        {
            Application.Current.MainWindow.Close();
        }
    }
}
